<template>
  <div>
    <el-dialog
      :title="form.uid ? '编辑管理员' : '添加管理员'"
      :visible.sync="isshow"
      @close="reset"
    >
      <el-form :model="form" :rules="rules" ref="adminForm">
        <el-form-item
          label="上级菜单"
          :label-width="formLabelWidth"
          prop="roleid"
        >
          <el-select v-model="form.roleid" placeholder="请选择上级菜单">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="用户名称"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            clearable
            v-model="form.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            show-password
            clearable
            type="password"
            v-model="form.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useraddedit, userinfo } from "../../request/api";
import { mapState, mapActions } from "vuex";
export default {
  props: ["pageData"],
  data() {
    return {
      isshow: false,
      dialogTableVisible: false,
      formLabelWidth: "120px",
      form: {
        roleid: "",
        username: "",
        password: "",
        status: "",
      },
      rules: {
        username: [{ required: true, message: "请输入标题", trigger: "blur" }],
        roleid: [
          { required: true, message: "请选择上级菜单", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapState("role", ["roleList"]),
    ...mapState("admin", ["userCount"]),
  },
  mounted() {
    if (this.roleList.length == 0) {
      this.getRolelist();
    }
  },
  methods: {
    ...mapActions("role", ["getRolelist"]),
    ...mapActions("admin", ["getUserCount", "getUserList"]),
    reset() {
      this.isshow = false;
      this.$refs.adminForm.resetFields();
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: "",
      };
    },
    submitForm() {
      this.$refs.adminForm.validate((val) => {
        if (!val) {
          return;
        }
        let url = this.form.uid ? "/useredit" : "/useradd";
        useraddedit(url, this.form).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            // if (this.form.uid) this.getUserList();
            this.getUserCount(this.form.uid ? false : true);
            this.reset();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
    getInfo(uid) {
      this.isshow = true;
      userinfo({ uid }).then((res) => {
        if (res.code == 200) {
          this.form = res.list;
          this.form.password = "";
        }
      });
    },
  },
};
</script>

<style scoped>
</style>